package com.generator.purchaseManage.productTypeManage.service.impl;

import com.generator.purchaseManage.productTypeManage.dao.IProductManageMapper;
import com.generator.purchaseManage.productTypeManage.pojo.CType;
import com.generator.purchaseManage.productTypeManage.service.IProductManageService;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/05
 * 星期三 23:26
 */
@Service
public class ProductManageService implements IProductManageService {

    @Autowired
    private IProductManageMapper productManageMapper;

    public IProductManageMapper getProductManageMapper() {
        return productManageMapper;
    }

    public void setProductManageMapper(IProductManageMapper productManageMapper) {
        this.productManageMapper = productManageMapper;
    }

    /**
     * 根据不同查询条件查询商品类型,并进行分页
     * @param cType
     * @return
     */
    @Override
    public PageModel<CType> getProductTypeByCondition(CType cType, PageModel<CType> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<CType> cTypeList = productManageMapper.queryProductTypeByCondition(cType);


        PageInfo<CType> pageInfo = new PageInfo<CType>(cTypeList);

        pageModel.setData(new ArrayList<CType>(cTypeList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }

    /**
     * 获取所有商品类型
     * @param cType
     * @return
     */
    @Override
    public List<CType> getProductTypeList(CType cType) {
        ArrayList<CType> list = new ArrayList<>();
        List<CType> cTypes = productManageMapper.queryProductTypeByCondition(cType);
        ArrayList<CType> cTypes1 = new ArrayList<>(cTypes);
        for (CType t : cTypes1) {
            if (inList(list,t)){
                continue;
            }
            list.add(t);
        }
        return list;
    }

    /**
     * 判断列表中是否有相同的类型名
     * @param list
     * @param c
     * @return
     */
    private boolean inList(ArrayList<CType> list,CType c){
        for (CType t : list) {
            if (c.getTypeName().equals(t.getTypeName())){
                return true;
            }
        }
        return false;
    }

    /**
     * 通过类型名称和型号查询商品类型
     * @param type
     * @return
     */
    @Override
    public CType queryProductTypeByName(CType type) {
        return productManageMapper.queryProductTypeByName(type);
    }

    /**
     * 添加商品类型
     * @param type
     * @return
     */
    @Override
    public boolean addProductType(CType type) {
        // 判断当前新增的商品类型是否存在
        CType t = new CType();
        t.setBrandName(type.getBrandName());
        t.setTypeName(type.getTypeName());
        List<CType> cTypes = productManageMapper.queryProductTypeByCondition(t);
        if (cTypes.size() != 0){
            return false;
        }
        return productManageMapper.addProductType(type);
    }

    /**
     * 修改商品类型信息
     * @param type
     * @return
     */
    @Override
    public boolean updateProductType(CType type) {
        return productManageMapper.updateProductType(type);
    }

    /**
     * 删除商品类型
     * @param type
     * @return
     */
    @Override
    public boolean deleteProductType(CType type) {
        return productManageMapper.deleteProductType(type);
    }
}
